home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ddj0190.arc / STOUT.LST < prev    next >
File List  |  1989-12-19  |  10KB  |  330 lines

  1. _S-CODER FOR DATA ENCRYPTION_
  2. by Robert Stout
  3.  
  4. [LISTIN╟ ONE]
  5.  
  6. /****************************************************************/
  7. /*      Simple S-CODER file encryptor/decryptor                 */
  8. /****************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <assert.h>
  13.  
  14. #define BUF_SIZ 32768
  15.  
  16. extern char *cryptext;
  17. extern int  crypt_length;
  18. void crypt(char *);
  19.  
  20. main(int argc, char *argv[])
  21. {
  22.         unsigned i, n;
  23.         char *buf, *p;
  24.         FILE *infile, *outfile;
  25.  
  26.         if (4 > argc)
  27.         {
  28.                 puts("\aUsage: CRYPT input_file output_file key");
  29.                 abort();
  30.         }
  31.         assert(buf = (char *)malloc(BUF_SIZ));
  32.         assert(infile  = fopen(argv[1], "rb"));
  33.         assert(outfile = fopen(argv[2], "wb"));
  34.         cryptext = argv[3];
  35.         crypt_length = strlen(cryptext);
  36.         while (n = fread(buf, 1, BUF_SIZ, infile))
  37.         {
  38.                 p = buf;
  39.                 for (i = 0; i < n; ++i)
  40.                         crypt(p++);
  41.                 fwrite(buf, 1, n, outfile);
  42.         }
  43.         fclose(infile);
  44.         fclose(outfile);è        exit(0);
  45. }
  46.  
  47.  
  48. [LISTIN╟ TWO]
  49.  
  50. /****************************************************************/
  51. /*      Simple S-CODER stream encryptor/decryptor               */
  52. /****************************************************************/
  53.  
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <assert.h>
  57.  
  58. extern char *cryptext;
  59. extern int  crypt_length;
  60. void crypt(char *);
  61.  
  62. main(int argc, char *argv[])
  63. {
  64.         char cch;
  65.         int ich;
  66.         FILE *infile;
  67.         void setraw(void);
  68.  
  69.         if (2 > argc)
  70.         {
  71.                 puts("\aUsage: SCRYPT key");
  72.                 puts("encrypts stdin to stdout");
  73.                 abort();
  74.         }
  75.         cryptext = argv[1];
  76.         crypt_length = strlen(cryptext);
  77.         setraw();  /* NOTE: setraw() will be compiler-dependent. It is used 
  78.                       to set stdin and stdout to raw binary mode. This is 
  79.                       necessary to avoid CR/LF translation and to avoid 
  80.                       sensing 0x1a as EOF during decryption. */
  81.         while (EOF != (ich = getchar()))
  82.         {
  83.                 cch = (char)ich;è                crypt(&cch);
  84.                 fputc(cch, stdout);
  85.         }
  86.         exit(0);
  87. }
  88.  
  89.  
  90.  
  91. [LISTIN╟ THREE] 
  92.  
  93. /****************************************************************/
  94. /*      Zortech C routine to set stin and stdout to binary mode */
  95. /****************************************************************/
  96.  
  97. #include <stdio.h>
  98.  
  99. extern FILE _iob[_NFILE];
  100.  
  101. void setraw(void)
  102. {
  103.         _iob[0]._flag &= ~_IOTRAN;
  104.         _iob[1]._flag &= ~_IOTRAN;
  105. }
  106.  
  107.  
  108.  
  109. [LISTIN╟ FOUR]
  110.  
  111. /****************************************************************/
  112. /*      Enhanced security S-CODER file encryptor/decryptor      */
  113. /****************************************************************/
  114.  
  115. #include <stdio.h>
  116. #include <stdlib.h>
  117. #include <string.h>
  118. #include <assert.h>
  119.  
  120. #define MIN_KEYL 6
  121.  
  122. extern char *cryptext;
  123. extern int  crypt_length;
  124. void crypt(char *);
  125. int cryptqual(void);
  126. long fsize;
  127. union {                         /* Transposition cipher buffer  */
  128.         char in[16384];
  129.         char out[128][128];
  130. } buf;
  131. FILE *infile, *outfile;
  132.  
  133. main(int argc, char *argv[])
  134. {
  135.         void encrypt(void);
  136.         void decrypt(void);
  137.  
  138.         if (5 > argc || NULL == strchr("EeDd", argv[1][0]))
  139.         {
  140.                 puts("\aUsage: HI-CRYPT { E | D } input_file output_file key");
  141.                 puts("where: E = Encrypt");
  142.                 puts("       D = Decrypt");
  143.                 abort();
  144.         }
  145.         assert(infile  = fopen(argv[2], "rb"));
  146.         assert(outfile = fopen(argv[3], "wb"));
  147.         cryptext = argv[4];
  148.         crypt_length = strlen(cryptext);
  149.         if (cryptqual())
  150.         {è                puts("\aHI-CRYPT: Key is not sufficiently complex");
  151.                 abort();
  152.         }
  153.         if (strchr("Ee", argv[1][0]))
  154.                 encrypt();
  155.         else    decrypt();
  156.         fclose(infile);
  157.         fclose(outfile);
  158.         exit(0);
  159. }
  160. int cryptqual(void)
  161. {
  162.         int i, j = 0;
  163.         static char found[MIN_KEYL + 1];   /* Statics initialized to zeros */
  164.  
  165.         if (6 > crypt_length)
  166.                 return -1;
  167.         for (i = 0; i < crypt_length; ++i)
  168.         {
  169.                 if (strchr(found, cryptext[i]))
  170.                         continue;
  171.                 found[j++] = cryptext[i];
  172.                 if ((MIN_KEYL - 1) < j)
  173.                         return 0;
  174.         }
  175.         return -1;
  176. }
  177. void encrypt(void)
  178. {
  179.         unsigned i, j, n;
  180.  
  181.         fseek(infile, 0L, SEEK_END);
  182.         fsize = ftell(infile);                          /* Save size    */
  183.         rewind(infile);
  184.         fwrite(&fsize, sizeof(long), 1, outfile);
  185.         srand((unsigned)fsize);
  186.         crypt_ptr = fsize % crypt_length;
  187.         while (n = fread(buf.in, 1, 16384, infile))
  188.         {
  189.                 while (16384 > n)
  190.                         buf.in[n++] = rand();
  191.                 for (i = 0; i < 128; ++i)
  192.                         for (j = 0; j < 128; ++j)
  193.                                 crypt(&buf.out[j][i]);
  194.                 fwrite(buf.in, 1, 16384, outfile);
  195.         }
  196. }
  197. void decrypt(void)
  198. {
  199.         unsigned i, j, n;
  200.         fread(&fsize, sizeof(long), 1, infile);è        crypt_ptr = fsize % crypt_length;
  201.         while (n = fread(buf.in, 1, 16384, infile))     /* Read size    */
  202.         {
  203.                 for (i = 0; i < 128; ++i)
  204.                         for (j = 0; j < 128; ++j)
  205.                                 crypt(&buf.out[j][i]);
  206.                 if (16384 <= fsize)
  207.                         fwrite(buf.in, 1, 16384, outfile);
  208.                 else    fwrite(buf.in, 1, fsize, outfile);
  209.                 fsize -= n;
  210.         }
  211. }
  212.  
  213.  
  214.  
  215. [LISTIN╟ FIVE]
  216.  
  217. /****************************************************************/
  218. /*      Collect file statistics                                 */
  219. /****************************************************************/
  220.  
  221. #include <stdio.h>
  222. #include <math.h>
  223. #include <assert.h>
  224.  
  225. main(int argc, char *argv[])
  226. {
  227.         int i, ch, hist = 0;
  228.         long n = 0L;
  229.         double mean = 0., stdev = 0., ftmp;
  230.         static unsigned bins[256];
  231.         FILE *infile;
  232.  
  233.         assert(infile = fopen(argv[1], "rb"));
  234.         while (!feof(infile))
  235.         {
  236.                 if (EOF == (ch = fgetc(infile)))
  237.                         break;
  238.                 bins[ch] += 1;
  239.                 ++n;
  240.         }
  241.         fclose(infile);
  242.         for (i = 0; i < 256; ++i)
  243.         {
  244.                 mean += (double)(bins[i]);
  245.                 if (bins[i])
  246.                         ++hist;
  247.         }
  248.         mean /= 256.;
  249.         for (i = 0; i < 256; ++i)
  250.         {
  251.                 ftmp = (double)(bins[i]) - mean;
  252.                 stdev += (ftmp * ftmp);
  253.         }
  254.         ftmp  = stdev / 255.;
  255.         stdev = sqrt(ftmp);
  256.         printf("%ld Characters were read from %s\n"è                "There are an average of %f occurances of each character\n"
  257.                 "%d Characters out of 256 possible were used\n"
  258.                 "The standard deviation is %f\n"
  259.                 "The coefficient of variation is %f%%\n",
  260.                 n, argv[1], mean, hist, stdev, (100. * stdev) / mean);
  261. }
  262.  
  263.         
  264.  
  265.  
  266. Figure 1: A polyalphabetic substitution cipher 
  267.  
  268.  
  269.  
  270. /****************************************************************/
  271. /¬ Simple encrypt/decrypt function using exclusive-ORing        */
  272. /¬ NOTE║ Thi≤ i≤ includeΣ fo≥ demonstratioε onlyí Datß encrypted¬/ 
  273. /¬ with this code will be subject to simple cryptanalysis.      */
  274. /****************************************************************/
  275. char *cryptext;                /* The encryption/decryption key */
  276. void crypt(char *buf)
  277. {
  278.         int crypt_ptr = 0;     /* Circular pointer to elements of key  */
  279.         *buf ^= cryptext[crypt_ptr];
  280.         if (++crypt_ptr >= strlen(cryptext))
  281.                 crypt_ptr = 0;
  282. }
  283.  
  284.  
  285.  
  286. Figure 2:  The S-CODER algorithm 
  287.  
  288. /****************************************************************/
  289. /*      S-CODER - Encrypt/decrypt data                          */
  290. /*      Copyright 1987-1989 by Robert B. Stout dba MicroFirm    */
  291. /*      Originally written by Bob Stout with modifications      */
  292. /*      suggested by Mike Smedley.                              */
  293. /*      This code may be used freely in any program for any     */
  294. /*      application, personal or commercial.                    */
  295. /*  Current commercial availability:                            */
  296. /*      1. MicroFirm Toolkit ver 3.00: LYNX and CRYPT utilities */
  297. /*      2. CXL libraries (MSC, TC, ZTC/C++, PC): fcrypt()       */
  298. /*         dedicated file encryption function                   */
  299. /*      3. SMTC & SMZT libraries: crypt() function              */
  300. /****************************************************************/
  301.  
  302. char *cryptext;         /* The actual encryption/decryption key */
  303. int   crypt_ptr = 0;    /* Circular pointer to elements of key  */
  304. int   crypt_length;     /* Set externally to strlen(cryptext)   */
  305.  
  306. /* NOTES: cryptext should be set and qualified (to something over
  307.           5-6 chars, minimum) by the calling program, which should
  308.           also set crypt_ptr in the range of 0 to strlen(cryptext)
  309.           before each use. If crypt() is used to encrypt several
  310.           buffers, cryptext should be reloaded and crypt_ptr reset
  311.           before each buffer is encrypted. The encryption is both
  312.           reversible - to decrypt data, pass it back through crypt()
  313.           using the original key and original initial value of
  314.           crypt_ptr - and multiple passes are commutative« */
  315.  
  316. /**** Encrypt/decrypt buffer datum ******************************/
  317. void crypt(char *buf)
  318. {
  319.         *buf ^= cryptext[crypt_ptr] ^ (cryptext[0] * crypt_ptr);
  320.         cryptext[crypt_ptr] += ((crypt_ptr < (crypt_length - 1)) ?
  321.                 cryptext[crypt_ptr + 1] : cryptext[0]);
  322.         if (!cryptext[crypt_ptr])
  323.                 cryptext[crypt_ptr] += 1;
  324.         if (++crypt_ptr >= crypt_length)
  325.                 crypt_ptr = 0;
  326. }
  327.  
  328.  
  329.  
  330.